<?php

class Setup extends Controller {

	private $view_data = array();

	public function __construct() {
		parent::__construct();
		
		$this->load->library('form_validation');
		$this->layout->resetTheme('cerberus');
		
		
		//load up any status messages
		$this->view_data['flashmessage'] = $this->db_session->flashdata('flashmessage');
		$this->view_data['flasherror'] = $this->db_session->flashdata('flasherror');
		
		$this->view_data['page_css'] = array('main');
		$this->view_data['page_js'] = array('jquery-1.2.6.pack', 'setup');
		
	}
	
	
	public function index() {
		
		$this->stepone();
	}
	
	public function stepone() {	
		$this->view_data['page_title'] = 'Step 1 &middot; Setup &middot; epicentr';
		
		$submit = $this->input->post('submit');
		
		$this->form_validation->set_rules('emailaddress', 'Email Address', 'trim|required|valid_email');
		
		if ( !$this->input->post('openid_identifier', true) ) $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');  //if no openid then password is required
		
		if ($submit && $this->form_validation->run()) {
			$this->load->model('usersmodel');

			$data = null;
		
			$data->emailaddress			= $this->input->post('emailaddress', true);
			$data->password				= $this->input->post('password', true);
			$data->openid_identifier	= $this->input->post('openid_identifier', true);
			
			$data->profiletype			= 'admin';
			
			$data->datecreated 			= date('Y-m-d H:i:s');
			$data->creatorid 			= 0;

			$userid = $this->usersmodel->add($data);


			redirect('setup/steptwo');
			
		} else {
		
			$this->layout->buildPage('setup/stepone', $this->view_data);
		}
	}
	
	public function steptwo() {
		$this->view_data['page_title'] = 'Step 2 &middot; Setup &middot; epicentr';
		
		$submit = $this->input->post('submit');
		
		$this->form_validation->set_rules('companyname', 'Company Name', 'trim|required');
		
	
		if ($submit && $this->form_validation->run()) {
			$this->load->model('companiesmodel');

			$data = null;
		
			$data->company_name		= $this->input->post('companyname', true);
			$data->yourcompany		= 1;
			$data->client 			= 1;
			$data->datecreated 		= date('Y-m-d H:i:s');
			$data->creatorid 		= 0;

			$companyid = $this->companiesmodel->add($data);

			redirect('');
		} else {
			$this->layout->buildPage('setup/steptwo', $this->view_data);
		}
	
	}
	
}

?>